package com.weilan.blog.picture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weilan.blog.base.enums.EStatus;
import com.weilan.blog.base.serviceImpl.SuperServiceImpl;
import com.weilan.blog.common.global.MessageConf;
import com.weilan.blog.common.global.SQLConf;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.picture.entity.Picture;
import com.weilan.blog.picture.entity.PictureCategory;
import com.weilan.blog.picture.mapper.PictureMapper;
import com.weilan.blog.picture.service.FileService;
import com.weilan.blog.picture.service.PictureCategoryService;
import com.weilan.blog.picture.service.PictureService;
import com.weilan.blog.picture.utils.WebUtil;
import com.weilan.blog.picture.vo.PictureVO;
import com.weilan.blog.utils.ResultUtil;
import com.weilan.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/20 10:26
 */
@Service
public class PictureServiceImpl extends SuperServiceImpl<PictureMapper, Picture> implements PictureService {

    @Autowired
    WebUtil webUtil;

    @Autowired
    private FileService fileService;

    @Autowired
    private PictureCategoryService pictureCategoryService;


    /**
     * 获取图片列表
     *
     * @param pictureVO
     * @return
     */
    @Override
    public IPage<Picture> getPageList(PictureVO pictureVO) {
        //封装查询条件
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pictureVO.getKeyword()) && !StringUtils.isEmpty(pictureVO.getKeyword().trim())) {
            queryWrapper.like(SQLConf.PIC_NAME, pictureVO.getKeyword().trim());
        }
        //封装分页条件
        Page<Picture> page = new Page<>();
        page.setCurrent(pictureVO.getCurrentPage());
        page.setSize(pictureVO.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        queryWrapper.eq(SQLConf.PICTURE_CATEGORY_UID, pictureVO.getPictureCategoryUid());
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        // 获取分页数据
        IPage<Picture> pageList = page(page, queryWrapper);
        List<Picture> pictureList = pageList.getRecords();
        final StringBuffer fileUids = new StringBuffer();
        pictureList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                fileUids.append(item.getFileUid() + SysConf.FILE_SEGMENTATION);
            }
        });
        String pictureResult = null;
        Map<String, String> pictureMap = new HashMap<>();

        // 获取文件
        if (fileUids != null) {
            pictureResult = fileService.getPicture(fileUids.toString(), SysConf.FILE_SEGMENTATION);
        }
        List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);

        picList.forEach(item -> {
            pictureMap.put(item.get(SysConf.UID).toString(), item.get(SysConf.URL).toString());
        });

        for (Picture item : pictureList) {
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                item.setPictureUrl(pictureMap.get(item.getFileUid()));
            }
        }
        pageList.setRecords(pictureList);
        return pageList;
    }

    @Override
    public String addPicture(List<PictureVO> pictureVOList) {
        ArrayList<Picture> pictureList = new ArrayList<>();
        if (pictureVOList.size() > 0) {
            for (PictureVO pictureVO : pictureVOList) {
                Picture picture = new Picture();
                picture.setFileUid(pictureVO.getFileUid());
                picture.setPictureCategoryUid(pictureVO.getPictureCategoryUid());
                picture.setPicName(pictureVO.getPicName());
                picture.setStatus(EStatus.ENABLE);
                picture.setCreateTime(LocalDateTime.now());
                picture.setUpdateTime(LocalDateTime.now());
                pictureList.add(picture);
            }
            saveBatch(pictureList);
        } else {
            return ResultUtil.result(SysConf.ERROR, MessageConf.INSERT_FAIL);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editPicture(PictureVO pictureVO) {
        Picture picture = getById(pictureVO.getUid());
        return null;
    }

    @Override
    public String deleteBatchPicture(Set<String> uids) {

        if(uids.isEmpty()){
            return ResultUtil.result(SysConf.ERROR, MessageConf.PARAM_INCORRECT);
        }
        for (String uid : uids) {
            Picture picture = getById(uid);
            picture.setStatus(EStatus.DISABLED);
            picture.setUpdateTime(LocalDateTime.now());
            picture.updateById();
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);

    }


    @Override
    public String setPictureCover(PictureVO pictureVO) {
        PictureCategory pictureCategory = pictureCategoryService.getById(pictureVO.getPictureCategoryUid());

        if(pictureCategory!=null){
            Picture picture = getById(pictureVO.getUid());
            if (picture != null) {
                pictureCategory.setFileUid(picture.getFileUid());
                picture.setUpdateTime(LocalDateTime.now());
                pictureCategory.updateById();
            } else {
                return ResultUtil.result(SysConf.ERROR, MessageConf.THE_PICTURE_NOT_EXIST);
            }
        }else {
            return ResultUtil.result(SysConf.ERROR, MessageConf.THE_PICTURE_SORT_NOT_EXIST);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public Picture getTopOne() {
        return null;
    }
}
